package com.class04.controller;

import com.class04.pojo.Schedule;
import com.class04.service.ScheduleService;
import com.class04.util.R;
import jakarta.ws.rs.PUT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/22 15:34
 */
@RestController
@RequestMapping("schedule")
@Slf4j
@CrossOrigin//允许其他资源访问我们的controller  浏览器不拦截此路径
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    //    todo
    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize, @PathVariable int currentPage) {
        R page = scheduleService.page(pageSize, currentPage);
        log.info("查询的数据为：{}", page);
        return page;
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        R r = scheduleService.remove(id);
        return r;
    }

    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("参数为空，不能保存");
        }
        R r = scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("参数为空，不能更新！");
        }
        R r = scheduleService.update(schedule);
        return r;
    }


}
